package com.hyts.mvc.base.interceptor;

import com.hyts.mvc.base.handler.DefaultWebMvcHandler;
import com.hyts.mvc.model.InterceptorWrapper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;

import java.util.List;
import java.util.stream.Collectors;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.mvc.base.interceptor
 * author:Libo/Alex
 * create-date:2019-07-05 13:32
 * email:liboware@gmail.com
 * description:此类主要用于:
 */
@AllArgsConstructor
public class WebMvcInterceptorStack extends ChainBase{

    /**
     * 拦截器栈
     */
    @Getter
    private List<InterceptorWrapper> interceptorStack;

    /**
     * 排序操作
     * @return
     */
    public WebMvcInterceptorStack build(){
        if(interceptorStack != null && !interceptorStack.isEmpty()){
            //添加对应的服务处理器类操作-每次都会创建
             interceptorStack.parallelStream().sorted((p1,p2)->p1.getOrder()-p2.getOrder()).
                     collect(Collectors.toList()).forEach(param->{
                         addCommand(param.getInterceptor());
             });

        }
        //TODO待优化
        addCommand(new DefaultWebMvcHandler());
        return this;
    }
}
